package com.grass.appointment.comment;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatDialog;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.appointment.C2694R;

public class InputTextDialog extends AppCompatDialog {
    private EditText ed_content;
    private InputMethodManager imm;
    private Context mContext;
    private int mLastDiff = 0;
    private OnTextSendListener mOnTextSendListener;

    public interface OnTextSendListener {
        void onTextSend(String str);
    }

    public InputTextDialog(Context context, int i) {
        super(context, i);
        this.mContext = context;
        getWindow().setWindowAnimations(C2694R.C2701style.PopAnimation);
        init();
        setLayout();
    }

    public void setHint(String str) {
        this.ed_content.setHint(str);
    }

    private void init() {
        setContentView(C2694R.C2699layout.dialog_input_text);
        this.ed_content = (EditText) findViewById(C2694R.C2697id.ed_content);
        LinearLayout linearLayout = (LinearLayout) findViewById(C2694R.C2697id.ll_input);
        final ImageView imageView = (ImageView) findViewById(C2694R.C2697id.img_send);
        this.ed_content.requestFocus();
        this.ed_content.addTextChangedListener(new TextWatcher() {
            /* class com.grass.appointment.comment.InputTextDialog.C27861 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (imageView == null) {
                    return;
                }
                if (charSequence.length() > 0) {
                    imageView.setImageResource(C2694R.C2696drawable.appointment_ic_comment_send_red);
                    imageView.setClickable(true);
                    return;
                }
                imageView.setImageResource(C2694R.C2696drawable.appointment_ic_comment_send_gray);
                imageView.setClickable(false);
            }
        });
        this.imm = (InputMethodManager) this.mContext.getSystemService("input_method");
        if (imageView != null) {
            imageView.setOnClickListener(new View.OnClickListener() {
                /* class com.grass.appointment.comment.InputTextDialog.View$OnClickListenerC27872 */

                public void onClick(View view) {
                    String trim = InputTextDialog.this.ed_content.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        InputTextDialog.this.mOnTextSendListener.onTextSend(trim);
                        InputTextDialog.this.imm.showSoftInput(InputTextDialog.this.ed_content, 2);
                        InputTextDialog.this.imm.hideSoftInputFromWindow(InputTextDialog.this.ed_content.getWindowToken(), 0);
                        InputTextDialog.this.ed_content.setText("");
                        InputTextDialog.this.dismiss();
                        return;
                    }
                    ToastUtils.getInstance().showSigh("請輸入評論");
                }
            });
        }
        this.ed_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /* class com.grass.appointment.comment.InputTextDialog.C27883 */

            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == 4) {
                    InputTextDialog.this.dismiss();
                    return false;
                } else if (i != 6 && i != 66) {
                    return false;
                } else {
                    String trim = InputTextDialog.this.ed_content.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        InputTextDialog.this.mOnTextSendListener.onTextSend(trim);
                        InputTextDialog.this.imm.showSoftInput(InputTextDialog.this.ed_content, 2);
                        InputTextDialog.this.imm.hideSoftInputFromWindow(InputTextDialog.this.ed_content.getWindowToken(), 0);
                        InputTextDialog.this.ed_content.setText("");
                        InputTextDialog.this.dismiss();
                        return true;
                    }
                    ToastUtils.getInstance().showSigh("請輸入評論");
                    return true;
                }
            }
        });
        this.ed_content.setOnKeyListener(new View.OnKeyListener() {
            /* class com.grass.appointment.comment.InputTextDialog.View$OnKeyListenerC27894 */

            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                Log.d("My test", "onKey " + keyEvent.getCharacters());
                return false;
            }
        });
        linearLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            /* class com.grass.appointment.comment.InputTextDialog.View$OnLayoutChangeListenerC27905 */

            public void onLayoutChange(View view, int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
                Rect rect = new Rect();
                InputTextDialog.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                int height = InputTextDialog.this.getWindow().getDecorView().getRootView().getHeight() - rect.bottom;
                if (height <= 0 && InputTextDialog.this.mLastDiff > 0) {
                    InputTextDialog.this.dismiss();
                }
                InputTextDialog.this.mLastDiff = height;
            }
        });
        linearLayout.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.appointment.comment.InputTextDialog.View$OnClickListenerC27916 */

            public void onClick(View view) {
                InputTextDialog.this.imm.hideSoftInputFromWindow(InputTextDialog.this.ed_content.getWindowToken(), 0);
                InputTextDialog.this.dismiss();
            }
        });
        setOnKeyListener(new DialogInterface.OnKeyListener() {
            /* class com.grass.appointment.comment.InputTextDialog.DialogInterface$OnKeyListenerC27927 */

            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i != 4 || keyEvent.getRepeatCount() != 0) {
                    return false;
                }
                InputTextDialog.this.dismiss();
                return false;
            }
        });
    }

    private void setLayout() {
        getWindow().setGravity(80);
        getWindow().getWindowManager().getDefaultDisplay();
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = -1;
        attributes.height = -2;
        getWindow().setAttributes(attributes);
        setCancelable(true);
        getWindow().setSoftInputMode(4);
    }

    public void setOnTextSendListener(OnTextSendListener onTextSendListener) {
        this.mOnTextSendListener = onTextSendListener;
    }

    public void dismiss() {
        super.dismiss();
        this.mLastDiff = 0;
    }

    public void show() {
        super.show();
    }
}
